package com.shop2cn.iapi.sqdistributor.testcase;

import java.util.List;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.shop2cn.iapi.sqdistributor.parameter.request.GetSubordinateDistributorListReq;
import com.shop2cn.iapi.sqdistributor.parameter.response.pojo.DistributorInfoVo;
import com.shop2cn.iapi.sqdistributor.service.GetSubordinateDistributorListCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.shenqi.SqDistributorService;
import com.ymttest.business.service.shenqi.bean.ShenqiUserSet;
import com.ymttest.database.model.sqdistributor.DistributorRelation;
import com.ymttest.database.sqlwapper.sqdistributor.DistributorRelationWrapper;
import com.ymttest.utils.DataUtil;

public class Ts_GetSubordinateDistributorList {
	
	//java接口
	private static GetSubordinateDistributorListReq getSubordinateDistributorListReq;
	private static GetSubordinateDistributorListCall getSubordinateDistributorListCall;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("获取下级分销员列表");
	}

	@Before
	public void caseUp() {
		getSubordinateDistributorListReq = new GetSubordinateDistributorListReq();
		getSubordinateDistributorListCall = new GetSubordinateDistributorListCall();	
	}
	
	@AfterClass
	public static void tearDown() {
		Logger.generateResult(getSubordinateDistributorListCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	public void verifyDb() throws Exception {
		
		DistributorRelationWrapper distributorRelationWrapper = new DistributorRelationWrapper();
		
		Long merchantId = SqDistributorService
				.getMasterShopId(getSubordinateDistributorListReq.getMerchantId());

		if (merchantId == 0L)
			merchantId = getSubordinateDistributorListReq.getMerchantId();

		List<DistributorRelation> actualResultList = distributorRelationWrapper
				.selectByMerchantIdAndParentDistributorId(
						merchantId,
						getSubordinateDistributorListReq
								.getParentDistributorId());
		List<DistributorInfoVo> expectResultList = getSubordinateDistributorListCall
				.getSubordinateDistributorListReponse()
				.getDistributorInfoVoList();

		Logger.verifyEquals(expectResultList.size(), actualResultList.size(),
				"验证列表大小");

		for (int i = 0; i < expectResultList.size(); i++) {

			DistributorRelation actualResult = actualResultList.get(i);
			DistributorInfoVo expectResult = expectResultList.get(i);

			Logger.verifyEquals(expectResult.getId(), actualResult.getId(),
					"验证id");
			Logger.verifyEquals(expectResult.getDistributorId(),
					actualResult.getDistributor_id(), "验证distributorId");
			Logger.verifyEquals(expectResult.getDistributorName(),
					actualResult.getDistributor_name(), "验证distributorName");
			Logger.verifyEquals(expectResult.getDistributorPhone(),
					actualResult.getDistributor_phone(), "验证distributorPhone");
			Logger.verifyEquals(expectResult.getDistributorLevelIndex(),
					actualResult.getDistributor_level_index(),
					"验证distributorLevelIndex");
			Logger.verifyEquals(expectResult.getDistributorStatus(),
					actualResult.getDistributor_status(), "验证distributorStatus");
			Logger.verifyEquals(expectResult.getParentDistributorId(),
					actualResult.getParent_distributor_id(),
					"验证parentDistributorId");
			Logger.verifyEquals(expectResult.getMerchantId(),
					actualResult.getMerchant_id(), "验证merchantId");
			DataUtil.verifyDateEquals(expectResult.getAddTime(),
					actualResult.getAdd_time(), "验证addTime");
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetSubordinateDistributorList_001() {
		Logger.start(true, "获取下级分销员列表,多条数据");
		
		try {
			
			getSubordinateDistributorListReq.setMerchantId(500865146L);
			getSubordinateDistributorListReq.setParentDistributorId(58082709L);
//			getSubordinateDistributorListReq.setDistributorId(distributorId);
//			getSubordinateDistributorListReq.setDistributorName(distributorName);
//			getSubordinateDistributorListReq.setDistributorPhone(distributorPhone);
//			getSubordinateDistributorListReq.setStartTime(startTime);
//			getSubordinateDistributorListReq.setEndTime(endTime);
			getSubordinateDistributorListReq.setPageIndex(1);
			getSubordinateDistributorListReq.setPageSize(10);
				
			getSubordinateDistributorListCall.setData(getSubordinateDistributorListReq);
			getSubordinateDistributorListCall.callService();
			
			Logger.verifyEquals(true, getSubordinateDistributorListCall.getSuccess(), "验证success");
			
			verifyDb();
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetSubordinateDistributorList_002() {
		Logger.start(true, "子店铺获取下级分销员列表,多条数据");
		
		try {
			
			List<Long> distributorList = SqDistributorService
					.buildSqDistributorRelation(ShenqiUserSet.autotest_goodsOwnerId, 0, 3, 0);
			
			getSubordinateDistributorListReq.setMerchantId(ShenqiUserSet.chainStoreGoodsOwnerId);
			getSubordinateDistributorListReq.setParentDistributorId(distributorList.get(0));
			getSubordinateDistributorListReq.setPageIndex(1);
			getSubordinateDistributorListReq.setPageSize(10);
				
			getSubordinateDistributorListCall.setData(getSubordinateDistributorListReq);
			getSubordinateDistributorListCall.callService();
			
			Logger.verifyEquals(true, getSubordinateDistributorListCall.getSuccess(), "验证success");
			
			verifyDb();
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}
